package com.imooc.imoocbilibili.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.imoocbilibili.entity.User;
import com.imooc.imoocbilibili.entity.UserInfo;
import com.imooc.imoocbilibili.entity.Video;
import com.imooc.imoocbilibili.enums.ErrorResultEnum;
import com.imooc.imoocbilibili.exception.RRException;
import com.imooc.imoocbilibili.interceptor.ThreadUser;
import com.imooc.imoocbilibili.service.UserInfoService;
import com.imooc.imoocbilibili.service.UserService;
import com.imooc.imoocbilibili.service.VideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.imooc.imoocbilibili.mapper.VideoCommentMapper;
import com.imooc.imoocbilibili.entity.VideoComment;
import com.imooc.imoocbilibili.service.VideoCommentService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service("videoCommentService")
@RequiredArgsConstructor
public class VideoCommentServiceImpl extends ServiceImpl<VideoCommentMapper, VideoComment> implements VideoCommentService {

    private final VideoService videoService;
    private final UserInfoService userInfoService;


    @Override
    public Page<VideoComment> queryPage(Integer pageNo, Integer pageSize, Long videoId) {
        Page<VideoComment> page = this.page(
                new Page<>(pageNo, pageSize),
                // 拼接查询条件
                new LambdaQueryWrapper<VideoComment>()
                .eq(VideoComment::getVideoid, videoId)
                .isNull(VideoComment::getRootid)
        );

        // 视频的所有一级评论
        List<VideoComment> records = page.getRecords();

        if (CollectionUtil.isNotEmpty(records)) {
            Set<Long> rootIds = records.stream().map(VideoComment::getId).collect(Collectors.toSet());
            // 所有二级评论
            List<VideoComment> allChildComments = this.list(new LambdaQueryWrapper<VideoComment>().in(VideoComment::getRootid, rootIds));
            // 查询一级,二级评论的用户
            Set<Long> userIds = records.stream().map(VideoComment::getUserid).collect(Collectors.toSet());
            Set<Long> replyUserIds = allChildComments.stream().map(VideoComment::getReplyuserid).collect(Collectors.toSet());
            userIds.addAll(replyUserIds);
            List<UserInfo> userList = userInfoService.list(new LambdaQueryWrapper<UserInfo>().in(UserInfo::getUserid, userIds));
            Map<Long, UserInfo> userMap = userList.stream().collect(Collectors.toMap(UserInfo::getUserid, Function.identity()));

            for (VideoComment comment : records) {
                List<VideoComment> childComments = allChildComments.stream().filter(child -> child.getRootid().equals(comment.getId())).collect(Collectors.toList());
                for (VideoComment childComment : childComments) {
                    childComment.setUserInfo(userMap.get(childComment.getUserid()));
                    childComment.setReplyUserInfo(userMap.get(childComment.getReplyuserid()));
                }

                comment.setChildList(childComments);
                comment.setUserInfo(userMap.get(comment.getUserid()));
            }
        }
        return page;
    }

    @Override
    public void addVideoComment(VideoComment videoComment) {
        Long videoId = videoComment.getVideoid();
        Video video = videoService.getById(videoId);
        if (video == null) {
            throw new RRException(ErrorResultEnum.PARAM_ERROR);
        }

        videoComment.setUserid(ThreadUser.getUser().getId());
        videoComment.setCreatetime(new Date());
        this.save(videoComment);
    }

}